#import "Sprite.h"
@implementation Sprite

- (Sprite*)initWithImage:(UIImage*)img mirror:(bool)m {
	if([super init]){
		mirror=m;
		//
        if(m)
           img = [UIImage imageWithCGImage:img.CGImage scale:1 orientation:UIImageOrientationUpMirrored];
    	[img retain];
        		images= [NSMutableArray arrayWithObject:img];
		[images retain];
	}
    return self;
	}
- (Sprite*)initWithArray:(NSArray*)imgs mirror:(bool)m {
	if([super init]){
		mirror=m;
		images= [NSMutableArray arrayWithArray:imgs];
		[images retain];
        if(m){
        for (int i=0;i<images.count;i++){
            UIImage* img =[images objectAtIndex:i];
            img =[UIImage imageWithCGImage:img.CGImage scale:1 orientation:UIImageOrientationUpMirrored];
            [images replaceObjectAtIndex:i withObject:img];
          }
        }
	}
        return self;
	}

- (Sprite*)initWithImage:(UIImage*)img withFrame:(int)f mirror:(bool)m {
	if([super init]){
		mirror=m;
		images= [NSMutableArray arrayWithCapacity:f];
        if(m)
			img = [UIImage imageWithCGImage:img.CGImage scale:1 orientation:UIImageOrientationUpMirrored];
		int h= img.size.height/f;
		for (int i=0; i<f; i++) {
			CGImageRef imgFr = CGImageCreateWithImageInRect(img.CGImage, CGRectMake(0, i*h, img.size.width, h));
			[images addObject: [UIImage imageWithCGImage:imgFr]];
			//[newImageRef ];
			
		}
		[images retain];
	}
    return self;
}
- (Sprite*)initWithImage:(UIImage*)img withRow:(int)r andColumn:(int)c mirror:(bool)m {
	if([super init]){
		mirror=m;
		images= [NSMutableArray arrayWithCapacity:r*c];
        if(m)
			img = [UIImage imageWithCGImage:img.CGImage scale:1 orientation:UIImageOrientationUpMirrored];
		int h= img.size.height/r;
		int w = img.size.width/c;
		
		for (int i=0; i<r; i++) {
			for (int j=0; j<c; j++) {
			CGImageRef imgFr = CGImageCreateWithImageInRect(img.CGImage, CGRectMake(j*w, i*h, w, h));
			[images addObject: [UIImage imageWithCGImage:imgFr]];
			}
		}
		[images retain];
	}
    return self;
}

- (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {  
    CGImageRef sourceImageRef = [image CGImage];  
    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);  
    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];  
    return newImage;  
}  
- (void) setCurrentFrame:(int)i {
    currentFrame=i%[images count];
}
- (int) getCurrentFrame {
    return currentFrame;
}
-(void) move:(int) x2 y:(int) y2 {
	CGPoint c;
		c.x = self.center.x+x2;
		c.y = self.center.y+y2;
		self.center = c;
	}
-(void)drawRect:(CGRect)rect{
	UIImage* img = [images objectAtIndex:currentFrame]; 
	[img drawInRect:rect];
}
	- (void)paint {
	UIImage* img = [images objectAtIndex:currentFrame]; 
		[img drawAtPoint:self.center ];
	}
	- (void) draw{
		[self paint];
	}
- (void) setPosition:(int) x2 y:(int) y2 {
	self.center=CGPointMake(x2,y2);
	}
-( int ) getWidth{
    UIImage* image =[images objectAtIndex:0];
		return image.size.width;
	}
-(CGImageRef)getCGImage:(int)f{
	UIImage* img = [images objectAtIndex:(f%[images count])];
	return img.CGImage;
}
-(UIImage*)getImage:(int)f{
	UIImage* img = [images objectAtIndex:(f%[images count])];
	return img;
}

-(int)count{
	return [images count];
}

@end
